<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 属性数据控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\service\ProjectCapacityReportService;
use think\Exception;
use think\Request;
use think\Response;


class ProjectCapacityReportController extends VerifyController
{
    /** @var ProjectCapacityReportService */
    protected $projectCapacityReportService;

    public function _initialize()
    {
        $this->projectCapacityReportService = ProjectCapacityReportService::getInstance();
    }

    /**
     * 通过分类查询项目产能上报情况
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function selectByStepCategory(Request $request)
    {
        $param = $request->param()['param'];
        $projectId = $param['project_id'];
        $stepCategoryIdList = $param['step_category_id_list'];
        $year = $param['year'];
        $res = $this->projectCapacityReportService->selectByStepCategory($projectId, $stepCategoryIdList, $year);
        return json(success_response("", $res));
    }

    /**
     * 通过分类查询项目产能上报情况
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function saveByStepCategory(Request $request)
    {
        $param = $request->param()['param'];
        $projectId = $param['project_id'];
        $stepCategoryIdList = $param['step_category_id_list'];
        $yearMonth = $param['year_month'];
        $yearMonthTimeStamp = strtotime($yearMonth . '-01 00:00:00');
        $config = $param['config'];
        $res = $this->projectCapacityReportService->saveByStepCategory($projectId, $stepCategoryIdList, $yearMonthTimeStamp, $config);
        return json(success_response("", $res));
    }

    /**
     * 产能显示分类
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function getShowStepCategoryList(Request $request)
    {
        $param = $request->param()['param'];
        $projectId = $param['project_id'];
        $res = $this->projectCapacityReportService->getShowStepCategoryList($projectId);
        return json(success_response("", $res));
    }

    /**
     * 产线产能配置折线
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function selectPlanRealData(Request $request)
    {
        $param = $request->param()['param'];
        $projectIdList = $param['project_id'];
        if (!is_array($projectIdList)) {
            $projectIdList = [$projectIdList];
        }
        $stepCategoryIdList = $param['step_category_id_list'];
        $startDate = strtotime($param['start_date']);
        $endDate = strtotime($param['end_date']);
        $categoryName = $param['category_name'] ?? '';
        $directorUserId = $param['director_user_id'] ?? 0;
        $res = $this->projectCapacityReportService->selectPlanRealData($projectIdList, $stepCategoryIdList, $startDate, $endDate, $categoryName, $directorUserId);
        return json(success_response("", $res));
    }

    /**
     * 单月产能详情统计
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function monthReportCount(Request $request)
    {
        $param = $request->param()['param'];
        $projectIdList = $param['project_id'];
        $stepCategoryIdList = $param['step_category_id_list'];
        $month = $param['month'] ?? '';
        $categoryName = $param['category_name'] ?? '';
        $directorUserId = $param['director_user_id'] ?? 0;
        $today = $param['today'] ?? 'no';
        if (!is_array($projectIdList)) {
            $projectIdList = [$projectIdList];
        }
        $res = $this->projectCapacityReportService->monthReportCount($projectIdList, $stepCategoryIdList, $month, $categoryName, $directorUserId, $today === 'yes');
        return json(success_response("", $res));
    }

    /**
     * 产能角色任务列表
     * @param Request $request
     * @return Response
     */
    public function monthReportTaskList(Request $request)
    {
        $param = $request->param()['param'];
        $filter = $param['filter'] ?? [];
        $projectIdList = $param['project_id'];
        $stepCategoryIdList = $param['step_category_id_list'];
        $month = $param['month'] ?? '';
        $categoryName = $param['category_name'] ?? '';
        $directorUserId = $param['director_user_id'] ?? 0;
        $page = $param['page'] ?? [1, C("database.database_max_select_rows")];
        $today = $param['today'] ?? 'no';
        if (!is_array($projectIdList)) {
            $projectIdList = [$projectIdList];
        }
        $res = $this->projectCapacityReportService->monthReportTaskList($projectIdList, $stepCategoryIdList, $month, $categoryName, $directorUserId, $today === 'yes', $filter, $page);
        return json(success_response("", $res));
    }

    /**
     * 月度产能上报情况 用户列表
     * @param Request $request
     * @return Response
     */
    public function monthReportUserList(Request $request)
    {
        $param = $request->param()['param'];
        $filter = $param['filter'] ?? [];
        $projectIdList = $param['project_id'];
        $stepCategoryIdList = $param['step_category_id_list'];
        $month = $param['month'] ?? '';
        $categoryName = $param['category_name'] ?? '';
        $directorUserId = $param['director_user_id'] ?? 0;
        $page = $param['page'] ?? [1, C("database.database_max_select_rows")];
        $today = $param['today'] ?? 'no';
        if (!is_array($projectIdList)) {
            $projectIdList = [$projectIdList];
        }
        $res = $this->projectCapacityReportService->monthReportUserList($projectIdList, $stepCategoryIdList, $month, $categoryName, $directorUserId, $today === 'yes', $filter, $page);
        return json(success_response("", $res));
    }

    /**
     * 用户任务列表
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function userTaskList(Request $request)
    {
        $param = $request->param()['param'];
        $month = $param['month'];
        $projectIdList = $param['project_id'] ?? 0;
        $stepCategoryIdList = $param['step_category_id_list'];
        if (!is_array($projectIdList)) {
            $projectIdList = [$projectIdList];
        }
        $res = $this->projectCapacityReportService->userTaskList($param, $projectIdList, $month, $stepCategoryIdList, $request->getUserId());
        return json(success_response("", $res));
    }

    /**
     * 计划产能折线
     * @param Request $request
     * @return Response
     */
    public function selectTaskPlanData(Request $request)
    {
        $param = $request->param()['param'];
        $projectIdList = $param['project_id'];
        if (!is_array($projectIdList)) {
            $projectIdList = [$projectIdList];
        }
        $stepCategoryIdList = $param['step_category_id_list'];
        $startDate = strtotime($param['start_date']);
        $endDate = strtotime($param['end_date']);
        $userInfo = [
            'department_id' => $param['department_id'] ?? null,
            'user_id' => $param['user_id'] ?? null,
        ];
        $res = $this->projectCapacityReportService->selectTaskPlanData($projectIdList, $stepCategoryIdList, $startDate, $endDate, $userInfo);
        return json(success_response("", $res));
    }

    /**
     * 查询项目每月产能情况
     * @param Request $request
     * @return Response
     */
    public function getProjectEntityNumberCountByMonth(Request $request)
    {
        $param = $request->param()['param'];
        $projectIds = $param['project_id'];
        $stepCategoryIdList = $param['step_category_id_list'];
        $startDate = strtotime($param['start_date']);
        $endDate = strtotime('+1 month -1 seconds', strtotime($param['end_date']));

        $res = $this->projectCapacityReportService->getProjectEntityNumberCountByMonth($projectIds, $stepCategoryIdList, $startDate, $endDate);
        return json(success_response("", $res));
    }

    /**
     * 查询项目每月产能情况
     * @param Request $request
     * @return Response
     */
    public function getProjectEntityCountMonthInfo(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->projectCapacityReportService->getProjectEntityCountMonthInfo($param);
        return json(success_response("", $res));
    }

    /**
     * 查询项目产能平均数
     * @param Request $request
     * @return Response
     */
    public function getProjectEntityNumberAvg(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->projectCapacityReportService->getPlanEpisodeAvgNumber($param['project_id'], $param['step_category_id_list']);
        return json(success_response("", $res));
    }

}
